require("guardian")

local enthack = Entity
function Entity(num)
	if num == 0 then return GetWorldEntity() end
	return enthack(num)
end

function ShouldTrace(ent1, ent2)
	if ent1 == ent2 then return false end
	local a, b = Entity(ent1), Entity(ent2)
	if !IsValid(a) || !IsValid(b) then return false end

	local ply, target
	if a:IsPlayer() then ply, target = a, b
	elseif b:IsPlayer() then ply, target = b, a
	else return false
	end

	if target:IsVehicle() && target != ply.Vech then
		return true
	end

	return false
end

ShouldCollide = ShouldTrace

function PostCollision(ent1, ent2, speed, delta, normal, prevel1, prevel2)
	if ent1 == ent2 then return false end
	local a, b = Entity(ent1), Entity(ent2)
	
	local veh, target
	if a:IsVehicle() then veh, target = a, b
	elseif b:IsVehicle() then veh, target = b, a
	else return
	end

	local normaldot = veh:GetUp():Dot(normal)
	
	if IsValid(target) || math.abs(normaldot) <= 0.95 then
		if prevel1:Length() > prevel2:Length() then
			GAMEMODE:VehicleCollision(b, a, speed)
		else
			GAMEMODE:VehicleCollision(a, b, speed)
		end
	else
		print(normaldot, speed)
	end
end